package com.fadadianqi.mall.web.controller;

import com.fadadianqi.commons.JSONProvider;
import com.fadadianqi.goods.model.Brand;
import com.fadadianqi.goods.service.GoodsService;
import com.fadadianqi.logistics.admin.controller.form.AjaxResult;
import com.fadadianqi.mall.product.SpuSearcher;
import com.fadadianqi.mall.product.model.ItemSPU;
import com.fadadianqi.search.Result;
import com.yeqiangwei.commons.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by aroot on 2/28/14.
 */
@Controller
public class SpuSearcherController {

    private static final Log log = LogFactory.getLog(SpuSearcherController.class);
    @Autowired
    private SpuSearcher spuSearcher;
    @Autowired
    private GoodsService goodsService;

    @RequestMapping(value = "/mall/ajax/search/spu/searcher")
    public ResponseEntity<String> ajaxSearcher(HttpServletRequest request, ModelMap modelMap) {
        String q = ParamUtils.getString(request, "q", null);
        Pagination pagination = new Pagination(request, 100);
        Result result = null;
        List<?> list = null;
        List<SpuVO> volist = new ArrayList<SpuVO>();
        try {
            result = spuSearcher.search(q, pagination);
            list = result.getItems();
            pagination.setTotal(result.getTotal());
            log.info("#search# keywords is: " + q + ", total: " + result.getTotal());
            Map<String, Object> elements = new HashMap<String, Object>();
            elements.put("q", q);
            pagination.setElements(elements);
            PaginationHelper.setAttribute(request, pagination);
            if (ValidatorHelper.isNotEmpty(list)) {
                for (int i = 0; i < list.size(); i++) {
                    ItemSPU spu = (ItemSPU) list.get(i);
                    SpuVO vo = new SpuVO();
                    BeanUtils.copyProperties(vo, spu);
                    vo.setBrand(goodsService.getBrandById(spu.getBrandId()));
                    volist.add(vo);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.toString());
        }
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");
        AjaxResult rs = new AjaxResult(volist, Boolean.TRUE, null);
        return new ResponseEntity<String>(JSONProvider.toJSON(rs),
                responseHeaders, HttpStatus.OK);
    }

    public class SpuVO extends ItemSPU {

        private Brand brand;

        public Brand getBrand() {
            return brand;
        }

        public void setBrand(Brand brand) {
            this.brand = brand;
        }
    }
}
